package cn.iocoder.yudao.module.famous.doctor.controller.admin.attention;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.attention.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.attention.AttentionDO;
import cn.iocoder.yudao.module.famous.doctor.convert.attention.AttentionConvert;
import cn.iocoder.yudao.module.famous.doctor.service.attention.AttentionService;

@Tag(name = "管理后台 - 我的关注")
@RestController
@RequestMapping("/famous.doctor/attention")
@Validated
public class AttentionController {

    @Resource
    private AttentionService attentionService;

    @PostMapping("/create")
    @Operation(summary = "创建我的关注")
    @PreAuthorize("@ss.hasPermission('famous.doctor:attention:create')")
    public CommonResult<Long> createAttention(@Valid @RequestBody AttentionCreateReqVO createReqVO) {
        return success(attentionService.createAttention(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新我的关注")
    @PreAuthorize("@ss.hasPermission('famous.doctor:attention:update')")
    public CommonResult<Boolean> updateAttention(@Valid @RequestBody AttentionUpdateReqVO updateReqVO) {
        attentionService.updateAttention(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除我的关注")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:attention:delete')")
    public CommonResult<Boolean> deleteAttention(@RequestParam("id") Long id) {
        attentionService.deleteAttention(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得我的关注")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:attention:query')")
    public CommonResult<AttentionRespVO> getAttention(@RequestParam("id") Long id) {
        AttentionDO attention = attentionService.getAttention(id);
        return success(AttentionConvert.INSTANCE.convert(attention));
    }

    @GetMapping("/list")
    @Operation(summary = "获得我的关注列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:attention:query')")
    public CommonResult<List<AttentionRespVO>> getAttentionList(@RequestParam("ids") Collection<Long> ids) {
        List<AttentionDO> list = attentionService.getAttentionList(ids);
        return success(AttentionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得我的关注分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:attention:query')")
    public CommonResult<PageResult<AttentionRespVO>> getAttentionPage(@Valid AttentionPageReqVO pageVO) {
        PageResult<AttentionDO> pageResult = attentionService.getAttentionPage(pageVO);
        return success(AttentionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出我的关注 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:attention:export')")
    @OperateLog(type = EXPORT)
    public void exportAttentionExcel(@Valid AttentionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AttentionDO> list = attentionService.getAttentionList(exportReqVO);
        // 导出 Excel
        List<AttentionExcelVO> datas = AttentionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "我的关注.xls", "数据", AttentionExcelVO.class, datas);
    }

}
